<template>
	<view>
		<view v-for="(goods, i) in goodList" :key="i">
			<my-goods :goods="goods" @click.native="gotoDetail(goods.goods_id)"></my-goods>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '', //查询的关键字
					cid: '', //商品分类id
					pagenum: 1, //分页码数
					pagesize: 10 //每次请求数据的条数
				},
				goodList: [], //商品列表
				total: 0, //总数据条数
				isLoading: false, //true  加载中 
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cat_id || ''
			this.getGoodsList()
		},
		// 上拉触底
		onReachBottom() {
			if (this.goodList.length >= this.total) {
				return uni.showToast({
					title: '数据请求完毕'
				})
			}
			// if(this.goodList.length>=this.total)  return uni.$showMsg('数据请求完毕')
			if (this.isLoading) return
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.goodList = []
			this.total = 0
			this.queryObj.pagenum = 1
			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			async getGoodsList(cb) {
				this.isLoading = true
				const {
					data
				} = await uni.$http.get('/goods/search', this.queryObj)
				this.goodList = [...this.goodList, ...data.message.goods]
				this.total = data.message.total
				this.isLoading = false
				typeof cb === 'function' && cb()
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			}
		},
	}
</script>
<style lang="scss">
</style>
